import { Injectable, Logger } from '@nestjs/common';

@Injectable()
export class OperationLoggerService {
  private readonly logger = new Logger('OperationLogger');

  // Redis 操作日志
  logRedisOperation(operation: string, key: string, error?: any) {
    if (error) {
      this.logger.error(`Redis ${operation} failed for key [${key}]:`, error);
    } else {
      this.logger.debug(`Redis ${operation} successful for key [${key}]`);
    }
  }

  // 用户操作日志
  logUserOperation(userId: number, operation: string, details?: any) {
    this.logger.log(`User [${userId}] performed ${operation}`, details);
  }

  // 系统操作日志
  logSystemOperation(operation: string, details?: any) {
    this.logger.log(`System operation: ${operation}`, details);
  }

  // 认证相关日志
  logAuthOperation(operation: string, username: string, success: boolean, details?: any) {
    if (success) {
      this.logger.log(`Auth success: ${operation} for user [${username}]`, details);
    } else {
      this.logger.warn(`Auth failed: ${operation} for user [${username}]`, details);
    }
  }

  // 错误日志
  logError(context: string, error: any, details?: any) {
    this.logger.error(`Error in ${context}:`, {
      error: error instanceof Error ? {
        message: error.message,
        stack: error.stack,
      } : error,
      details,
    });
  }
}